package com.three;

import android.content.Context;
import android.graphics.Bitmap;

import com.fast.dev.frame.glide.BitmapUtils;
import com.fast.dev.frame.utils.StringUtils;
import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
import com.tencent.mm.sdk.modelmsg.WXImageObject;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXMusicObject;
import com.tencent.mm.sdk.modelmsg.WXTextObject;
import com.tencent.mm.sdk.modelmsg.WXVideoObject;
import com.tencent.mm.sdk.modelmsg.WXWebpageObject;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.wxtt.android.config.ConstantConfig;

/**
 * 说明：微信操作的工具类
 * <p/>
 * 作者：fanly
 * <p/>
 * 时间：2016/2/25 11:15
 * <p/>
 * 版本：verson 1.0
 */
public class WxUtils {

    private static final String APP_ID = ConstantConfig.WX_APPID;

    //场景类型
    public static final int SCENE_SESSION = 0;//发送到聊天界面WXSceneSession
    public static final int SCENE_TIMELINE = 1;//发送到朋友圈WXSceneTimeline
//    public static final int SCENE_FAVORITE = 2;//发送到微信收藏WXSceneFavorite

    private static final int THUMB_SIZE = 150;

    //错误提示
    private static final String NOT_INSTALL = "未安装微信客户端";
    private static final String NOT_SUPPORT = "微信客户端不支持";
    private static final String CONTEXT_IS_NULL = "context 不能为 null";
    private static final String SCENE_ERROR = "微信场景类型错误";

    private static final String SHARE_TEXT_NULL = "分享内容为空";

    private static final String SHARE_IMAGE_NULL = "分享图片错误";

    private static final String SHARE_MUSIC_URL_NULL = "分享音乐链接为空";
    private static final String SHARE_MUSIC_TITLE_NULL = "分享音乐标题为空";
    private static final String SHARE_MUSIC_DES_NULL = "分享音乐描述为空";
    private static final String SHARE_MUSIC_BITMAP_NULL = "分享音乐图为空";

    private static final String SHARE_VIDEO_URL_NULL = "分享视频链接为空";
    private static final String SHARE_VIDEO_TITLE_NULL = "分享视频标题为空";
    private static final String SHARE_VIDEO_DES_NULL = "分享视频描述为空";
    private static final String SHARE_VIDEO_BITMAP_NULL = "分享视频图为空";

    private static final String SHARE_PAGE_URL_NULL = "分享网页链接为空";
    private static final String SHARE_PAGE_TITLE_NULL = "分享网页标题为空";
    private static final String SHARE_PAGE_DES_NULL = "分享网页描述为空";
    private static final String SHARE_PAGE_BITMAP_NULL = "分享网页图为空";

    /**
     * 说明：注册到微信
     * @param context
     */
    public synchronized static IWXAPI regToWx(Context context){
        IWXAPI api = WXAPIFactory.createWXAPI(context,APP_ID,true);
        api.registerApp(APP_ID);
        return api;
    }

    /**
     * 说明：是否安装微信客户端
     * @return
     */
    public static boolean isInstall(Context context){
        return regToWx(context).isWXAppInstalled();
    }

    /**
     * 说明：微信客户端是否支持
     * @return
     */
    public static boolean isSupport(Context context){
        return regToWx(context).isWXAppSupportAPI();
    }


    /**
     * 说明：创建Req
     * @param msg
     * @param type
     * @return
     */
    private static SendMessageToWX.Req createReq(WXMediaMessage msg,int type){
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.message = msg;
        req.transaction = String.valueOf(System.currentTimeMillis());
        switch (type){
            case SCENE_SESSION:
                req.scene = SendMessageToWX.Req.WXSceneSession;
                break;
            case SCENE_TIMELINE:
                req.scene = SendMessageToWX.Req.WXSceneTimeline;
                break;
//            case SCENE_FAVORITE:
//                req.scene = SendMessageToWX.Req.WXSceneFavorite;
//                break;
            default:
                throw new RuntimeException(SCENE_ERROR);
        }
        return req;
    }

    /**
     * 说明：分享（文字类型分享）
     * @param context
     * @param text
     * @param type
     * @return
     */
    public static String shareText(Context context,String text,int type){
        if (context == null){
            return CONTEXT_IS_NULL;
        }
        if (StringUtils.isEmpty(text)){
            return SHARE_TEXT_NULL;
        }
        IWXAPI shareApi = regToWx(context);
        if (!shareApi.isWXAppInstalled()){
            return NOT_INSTALL;
        }
        if (!shareApi.isWXAppSupportAPI()){
            return NOT_SUPPORT;
        }

        WXTextObject textObject = new WXTextObject();
        textObject.text = text;

        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = textObject;
        msg.description = text;

        shareApi.sendReq(createReq(msg,type));
        return "";
    }

    /**
     * 说明：分享（图片类型分享）
     * @param context
     * @param bitmap
     * @param type
     * @return
     */
    public static String shareImage(Context context,Bitmap bitmap,int type){
        if (context == null){
            return CONTEXT_IS_NULL;
        }
        if (bitmap == null){
            return SHARE_IMAGE_NULL;
        }
        IWXAPI shareApi = regToWx(context);
        if (!shareApi.isWXAppInstalled()){
            return NOT_INSTALL;
        }
        if (!shareApi.isWXAppSupportAPI()){
            return NOT_SUPPORT;
        }

        WXImageObject imageObject = new WXImageObject(bitmap);

        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = imageObject;

        Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap,THUMB_SIZE,THUMB_SIZE,true);
        bitmap.recycle();
        msg.thumbData = BitmapUtils.bmpToByteArray(thumbBmp,true);

        shareApi.sendReq(createReq(msg,type));
        return "";
    }

    /**
     * 说明：分享（音乐类型分享）
     * @param context
     * @param url 音乐链接
     * @param title 音乐标题
     * @param description 音乐描述
     * @param bitmap 缩略图
     * @param type
     * @return
     */
    public static String shareMusic(Context context,String url,String title,String description,Bitmap bitmap,int type){
        if (context == null){
            return CONTEXT_IS_NULL;
        }
        if (StringUtils.isEmpty(url)){
            return SHARE_MUSIC_URL_NULL;
        }
        if (StringUtils.isEmpty(title)){
            return SHARE_MUSIC_TITLE_NULL;
        }
        if (StringUtils.isEmpty(description)){
            return SHARE_MUSIC_DES_NULL;
        }
        if (bitmap == null){
            return SHARE_MUSIC_BITMAP_NULL;
        }
        IWXAPI shareApi = regToWx(context);
        if (!shareApi.isWXAppInstalled()){
            return NOT_INSTALL;
        }
        if (!shareApi.isWXAppSupportAPI()){
            return NOT_SUPPORT;
        }

        WXMusicObject musicObject = new WXMusicObject();
        musicObject.musicUrl = url;

        Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap,THUMB_SIZE,THUMB_SIZE,true);
        bitmap.recycle();

        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = musicObject;
        msg.title = title;
        msg.description = description;
        msg.thumbData = BitmapUtils.bmpToByteArray(thumbBmp,true);

        shareApi.sendReq(createReq(msg,type));
        return "";
    }

    /**
     * 说明：分享（视频类型分享）
     * @param context
     * @param url 视频链接
     * @param title 视频标题
     * @param description 视频描述
     * @param bitmap 缩略图
     * @param type
     * @return
     */
    public static String shareVideo(Context context,String url,String title,String description,Bitmap bitmap,int type){
        if (context == null){
            return CONTEXT_IS_NULL;
        }
        if (StringUtils.isEmpty(url)){
            return SHARE_VIDEO_URL_NULL;
        }
        if (StringUtils.isEmpty(title)){
            return SHARE_VIDEO_TITLE_NULL;
        }
        if (StringUtils.isEmpty(description)){
            return SHARE_VIDEO_DES_NULL;
        }
        if (bitmap == null){
            return SHARE_VIDEO_BITMAP_NULL;
        }
        IWXAPI shareApi = regToWx(context);
        if (!shareApi.isWXAppInstalled()){
            return NOT_INSTALL;
        }
        if (!shareApi.isWXAppSupportAPI()){
            return NOT_SUPPORT;
        }

        WXVideoObject videoObject = new WXVideoObject();
        videoObject.videoUrl = url;

        Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap,THUMB_SIZE,THUMB_SIZE,true);
        bitmap.recycle();

        WXMediaMessage msg = new WXMediaMessage(videoObject);
        msg.title = title;
        msg.description = description;
        msg.thumbData = BitmapUtils.bmpToByteArray(thumbBmp,true);

        shareApi.sendReq(createReq(msg,type));
        return "";
    }

    /**
     * 说明：分享（网页类型分享）
     * @param context
     * @param url 视频链接
     * @param title 视频标题
     * @param description 视频描述
     * @param bitmap 缩略图
     * @param type
     * @return
     */
    public static String sharePage(Context context,String url,String title,String description,Bitmap bitmap,int type){
        if (context == null){
            return CONTEXT_IS_NULL;
        }
        if (StringUtils.isEmpty(url)){
            return SHARE_PAGE_URL_NULL;
        }
        if (StringUtils.isEmpty(title)){
            return SHARE_PAGE_TITLE_NULL;
        }
        if (StringUtils.isEmpty(description)){
            return SHARE_PAGE_DES_NULL;
        }
        if (bitmap == null){
            return SHARE_PAGE_BITMAP_NULL;
        }
        IWXAPI shareApi = regToWx(context);
        if (!shareApi.isWXAppInstalled()){
            return NOT_INSTALL;
        }
        if (!shareApi.isWXAppSupportAPI()){
            return NOT_SUPPORT;
        }

        WXWebpageObject pageObject = new WXWebpageObject();
        pageObject.webpageUrl = url;

        Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap,THUMB_SIZE,THUMB_SIZE,true);
        bitmap.recycle();

        WXMediaMessage msg = new WXMediaMessage(pageObject);
        msg.title = title;
        msg.description = description;
        msg.thumbData = BitmapUtils.bmpToByteArray(thumbBmp,true);

        shareApi.sendReq(createReq(msg,type));
        return "";
    }

}
